function new_image = tpsWarp(target, source, old_img)

% compute mapping from target to source
W = thinPlateSpline(target, source);

% sample points in target space and warp them to source space
[X Y] = meshgrid(1:size(old_img, 1), 1:size(old_img, 2));
new_points = thinPlateWarp([Y(:) X(:)], target, W);

% interpolate colour in source image and store them in target image
new_image = zeros(size(old_img));
for i = 1:size(old_img, 3)
    new_image(:, :, i) = reshape(interp2(double(old_img(:, :, i))/255, new_points(:, 1), new_points(:, 2), 'bilinear*'), size(old_img, 2), size(old_img, 1))';
end
